package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private HttpSession  session ;

    /**
     * 处理菜品添加请求
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        //获取登陆人id
        Long id = (Long) session.getAttribute("employee");

        //完善修改人,创建人
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);

        //调用业务添加
        dishService.save(dishDto);

        //返回数据
        return R.success("添加成功");
    }


    /**
     * 处理分页查询请求
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto >> findByPage(Integer page,Integer pageSize,String name){
        //调用业务层获取分页数据
        Page<DishDto> byPage = dishService.findByPage(page, pageSize, name);
        //返回
        return R.success(byPage);
    }

    /**
     * 处理根据id来查询指定菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        //调用业务层获取分页数据
        DishDto byId = dishService.findById(id);
        //封装返回
        return R.success(byId);
    }


    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        //1.获取当前登录者
        Long id = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(id);
        dishService.updateById(dishDto);
        return R.success("修改菜品成功");
    }


    /**
     * 处理根据类别id查询菜品列表的请求
     * @param categoryId
     * @return
     */
 /*   @RequestMapping("/list")
    public R<List<Dish>> findByCategoryId(Long categoryId){
        //1.调用业务查询类别id的菜品列表
        List<Dish> byCategoryId = dishService.findByCategoryId(categoryId);

        //2.封装数据返回
        return R.success(byCategoryId);
    }*/


    @RequestMapping("/list")
    public R<List<DishDto>> findByCategoryId(Long categoryId,Integer status){
          //调用业务查询类别id的菜品列表
        List<DishDto> dishDtoList = dishService.findByCategoryId(categoryId, status);
        //封装数据返回
        return R.success(dishDtoList);

    }
}
